package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.SchoolRecruitChannelsAddDTO;
import com.oa.pm.dto.SchoolRecruitChannelsListParamDTO;
import com.oa.pm.entity.SchoolRecruitChannels;
import com.oa.pm.mapper.SchoolRecruitChannelsMapper;
import com.oa.pm.service.ISchoolRecruitChannelsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.service.ISchoolRecruitmentContactRecordService;
import com.oa.pm.vo.SchoolRecruitChannelsDetailsVO;
import com.oa.pm.vo.SchoolRecruitChannelsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 校招渠道管理表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class SchoolRecruitChannelsServiceImpl extends ServiceImpl<SchoolRecruitChannelsMapper, SchoolRecruitChannels> implements ISchoolRecruitChannelsService {

    private final SchoolRecruitChannelsMapper schoolRecruitChannelsMapper;

    @Autowired
    private ISchoolRecruitmentContactRecordService schoolRecruitmentContactRecordService;

    @Autowired
    public SchoolRecruitChannelsServiceImpl(SchoolRecruitChannelsMapper schoolRecruitChannelsMapper) {
        this.schoolRecruitChannelsMapper = schoolRecruitChannelsMapper;
    }

    @Override
    @CommonService
    public PageResult<SchoolRecruitChannels> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<SchoolRecruitChannels> list = schoolRecruitChannelsMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<SchoolRecruitChannels> page = new Page<>(pageNum, pageSize);
            //查询
            List<SchoolRecruitChannels> list = schoolRecruitChannelsMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public SchoolRecruitChannelsDetailsVO findSchoolRecruitChannelsById(String id){
        return schoolRecruitChannelsMapper.findSchoolRecruitChannelsDetails(id);
    }

    @Override
    public PageResult<SchoolRecruitChannelsListVO> findSchoolRecruitChannelsList(SchoolRecruitChannelsListParamDTO schoolRecruitChannelsListParamDTO) {
        return new PageResult<>(schoolRecruitChannelsMapper.findSchoolRecruitChannelsList(schoolRecruitChannelsListParamDTO.getPage(), schoolRecruitChannelsListParamDTO));
    }

    @Transactional
    @Override
    public boolean saveSchoolRecruitChannels(SchoolRecruitChannelsAddDTO schoolRecruitChannelsAddDTO) {

        if(schoolRecruitChannelsAddDTO.getSchoolRecruitChannels().getId() != null){
            Wrapper wrapper = new EntityWrapper();
            wrapper.where("school_recruit_channels_id = {0}", schoolRecruitChannelsAddDTO.getSchoolRecruitChannels().getId());
            schoolRecruitmentContactRecordService.delete(wrapper);
        }
        if(insertOrUpdate(schoolRecruitChannelsAddDTO.getSchoolRecruitChannels())){
            if(schoolRecruitChannelsAddDTO.getSchoolRecruitmentContactRecords() != null && schoolRecruitChannelsAddDTO.getSchoolRecruitmentContactRecords().size() > 0){
                schoolRecruitChannelsAddDTO.getSchoolRecruitmentContactRecords().forEach(d -> d.setSchoolRecruitChannelsId(schoolRecruitChannelsAddDTO.getSchoolRecruitChannels().getId()));
                if(schoolRecruitmentContactRecordService.insertOrUpdateBatch(schoolRecruitChannelsAddDTO.getSchoolRecruitmentContactRecords())){
                    return true;
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void export(SchoolRecruitChannelsListParamDTO schoolRecruitChannelsListParamDTO, HttpServletResponse response) {
        List<SchoolRecruitChannelsListVO> schoolRecruitChannelsListVOS;
        String[] rows = {"id", "省份", "城市", "学校名称", "联系人", "联系电话", "邮箱", "合作状态"};
        if(schoolRecruitChannelsListParamDTO.getPage() != null){
            Page page = schoolRecruitChannelsListParamDTO.getPage();
            schoolRecruitChannelsListVOS = schoolRecruitChannelsMapper.findSchoolRecruitChannelsList(page, schoolRecruitChannelsListParamDTO);
        }else{
            schoolRecruitChannelsListVOS = schoolRecruitChannelsMapper.findSchoolRecruitChannelsList(new Page(), schoolRecruitChannelsListParamDTO);
        }

        try {
            ExcelUtil.downloadExcel("校招渠道列表" + DateUtil.getDayMonth(), rows, schoolRecruitChannelsListVOS, "校招渠道列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
